package smetana.modules.business;

import smetana.domain.join.DocPositionReport;
import smetana.domain.raw.Doc;
import smetana.domain.raw.DocPosition;
import smetana.mapper.join.ReportMapper;
import smetana.mapper.raw.DocPositionMapper;
import smetana.modules.business.meta.Commit;

/**
 * @author dmitry.mamonov
 */
public class BusinessDocPositionImpl extends BaseImpl implements BusinessDocPosition{
    @Override
    @Commit
    public DocPosition newDocPosition(Doc doc) {
        DocPositionMapper mapper = sql.getMapper(DocPositionMapper.class);
        DocPosition docPosition = new DocPosition(mapper.nextId());
        docPosition.setDocId(doc.getId());
        mapper.insert(docPosition);
        return mapper.findById(docPosition.getId());
    }

    @Override
    public DocPositionReport findDocPositionReport(Integer documentItemId) {
        if (documentItemId == null) {
            return null;
        } else {
            ReportMapper mapper = sql.getMapper(ReportMapper.class);
            return mapper.findDocumentItem(documentItemId);
        }
    }

    @Override
    @Commit
    public void saveDocPosition(DocPosition documentItem) {
        DocPositionMapper mapper = sql.getMapper(DocPositionMapper.class);
        mapper.update(documentItem);
    }

    @Override
    public java.util.List<DocPositionReport> listDocPositionReports(Doc doc) {
        ReportMapper mapper = sql.getMapper(ReportMapper.class);
        return mapper.listDocPositionReports(doc.getId());
    }
    

    @Override
    @Commit
    public void deleteDocPosition(DocPosition docItem) {
        if (docItem != null) {
            DocPositionMapper mapper = sql.getMapper(DocPositionMapper.class);
            mapper.delete(docItem.getId());
        }
    }

}
